package com.spider.admin.agent;

import com.spider.admin.agent.conf.AdminWebConf;
import com.spider.admin.agent.conf.CommonConf;
import com.spider.admin.agent.conf.GlobalConf;
import com.spider.admin.agent.conf.HttpConf;
import com.spider.admin.conf.ENVConf;
import com.spider.admin.utils.CommonUtil;
import com.spider.admin.utils.FileUtils;
import com.spider.admin.utils.OSCommandUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

@Slf4j
@Component
public class WebNginxConf {
    @Autowired
    private ENVConf envConf;


    public void writeWebConf() {
        try {
            String commonConfPath = null;
            if (OSCommandUtil.isWinOS()) {
                commonConfPath = envConf.getUiContainerBasePath() + "/conf/nginx.conf";
            } else {
                commonConfPath = envConf.getUiContainerBasePath() + "/nginx/conf/nginx.conf";
            }
            FileUtils.writeFile(commonConfPath, getWebConfInfo());
            log.info("生成HTTP配置");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("HTTP配置生成异常：" + e.getMessage());
        }
    }


    private String getWebConfInfo() {

        String dev_conf = "server {\n" +
                "\t\tlisten       80;\n" +
                "\t\tserver_name  admin-dev.duli123.com;\n" +
                "\t\tlocation / {\n" +
                "\t\t\tproxy_pass   http://127.0.0.1:9900;\n" +
                "\t\t}\n" +
                "\t\tlocation /api/ {  \n" +
                "\t\t\taccess_by_lua_file hgg-lua/access_control.lua;          \n" +
                "\t\t\tproxy_pass   http://managerApiServer/;\n" +
                "\t\t}\n" +
                "\t\tlocation /verify { #验证token服务\n" +
                "\t\t\tinternal;           #内部location，不能外部调用\n" +
                "\t\t\tproxy_pass   http://managerApiServer/verify;\n" +
                "\t\t\tproxy_set_header Content-type \"application/json\";\n" +
                "\t\t}\n" +
                "\t}";
        String default_conf = "\n\tserver {\n" +
                "\t\tlisten       80;\n" +
                "\t\tserver_name  localhost;\n" +
                "\n" +
                "\t\tlocation / {\n" +
                "\t\t\tproxy_pass   http://127.0.0.1:" + envConf.getAgentRunPort() + "/;\n" +
                "\t\t\tproxy_set_header Host $host;\n" +
                "\t\t\tproxy_set_header X-Real-IP $remote_addr;\n" +
                "\t\t\tproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n" +
                "\t\t\tproxy_set_header X-Forwarded-Proto $scheme;\n"+
                "\t\t}\n" +
                "\t}";
        String pro_conf =
                "#user  spider;\n" +
                        "worker_processes  1;\n" +
                        "\n" +
                        "#error_log  logs/error.log;\n" +
                        "#error_log  logs/error.log  notice;\n" +
                        "#error_log  logs/error.log  info;\n" +
                        "\n" +
                        "\n" +
                        "#pid        logs/nginx.pid;\n" +
                        "\n" +
                        "events {\n" +
                        "    worker_connections  1024;\n" +
                        "}\n" +
                        "http {\n" +
                        "    include       mime.types;\n" +
                        "    charset utf-8;\n" +
                        "    charset_types text/css text/plain application/json application/javascript;\n" +
                        "    resolver 8.8.8.8;\n" +
                        "    log_format  main  '$remote_addr - \"$request\" ';\n" +
                        "\n" +
                        "    #access_log  logs/access.log  main;\n" +
                        "    sendfile        on;\n" +
                        "    #tcp_nopush     on;\n" +
                        "\n" +
                        "    #keepalive_timeout  0;\n" +
                        "    keepalive_timeout  65;\n" +
                        "    \n" +
                        "    #lua_shared_dict logs_data 1m;\n" +
                        "    #init_worker_by_lua_file hgg-lua/timer.lua;\n" +
                        "    #gzip  on;\n" +
                        "\n" +
                        "\n" +
                        "\tupstream managerApiServer {\n" +
                        "\t\tserver 127.0.0.1:" + envConf.getServerPort() + ";\n" +
                        "\t}\n" +
                        default_conf
                        +
                        "\n\tserver {\n" +
                        "\t\tlisten       " + envConf.getUiRunPort() + ";\n" +
                        "\t\tserver_name  admin.duli123.com;\n" +
                        "\n" +
                        "\t\tlocation / {\n" +
                        "\t\t\troot html;\n" +
                        "\t\t\tindex index.html index.htm;\n" +
                        "\t\t}\n" +
                        "\n" +
                        "\t\tlocation /api/ {\n" +
                        "\t\t\taccess_by_lua_file hgg-lua/access_control.lua;\n" +
                        "\t\t\tproxy_pass   http://managerApiServer/;\n" +
                        "\t\t}\n" +
                        "\t\tlocation /verify { #验证token服务\n" +
                        "\t\t\tinternal;           #内部location，不能外部调用\n" +
                        "\t\t\tproxy_pass   http://managerApiServer/verify;\n" +
                        "\t\t\tproxy_set_header Content-type \"application/json\";\n" +
                        "\t\t}\n" +
                        "\t}\n\t" +
                        dev_conf +
                        "\n}";
        return pro_conf;
    }
}
